package com.ccmusic.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ccmusic.Message.Msg;
import com.ccmusic.entity.Comments;
import com.ccmusic.service.CommentsService;
import com.ccmusic.service.impl.CommentsServiceImpl;
import com.ccmusic.util.DatatablesBean;
import com.google.gson.Gson;

/**
 * 评论的servlet
 * 作者：cyf
 * Servlet implementation class CommentsServlet
 */
@WebServlet("/CommentsServlet")
public class CommentsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       CommentsService cs=new CommentsServiceImpl();
 
	/**
	 * 
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//设置请求编码格式
		request.setCharacterEncoding("utf-8");
		//接收op传过来的参数，并判断执行哪个方法
		String op="";
		if(request.getParameter("op")!=null) {
			op=request.getParameter("op");
		}
		
		if("query".equals(op)) {
			doquery(request,response);
		}else if("delcomments".equals(op)) {
			dodelcomments(request,response);
		}else if("addcomments".equals(op)) {
			doaddcomments(request,response);
		}
	
		
	}

	
	/**
	 * 用datatable展示所有评论的方法
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void doquery(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//设置响应编码 
		response.setCharacterEncoding("utf-8");
		//设置响应数据类型
		response.setContentType("application/json");
		//获取所有评论数据
		DatatablesBean<Comments> data=cs.getAllComments();
		//将获得的json格式的数据转成json类型的数据传回去
		Gson gson=new Gson();
		String str=gson.toJson(data);
		PrintWriter out=response.getWriter();
		out.print(str);
		out.close();
	}
	/**
	 * 将前端页面用户的评论信息添加到数据库
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void doaddcomments(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//设置请求编码 
		request.setCharacterEncoding("utf-8");
		//设置响应编码 
		response.setCharacterEncoding("utf-8");
		//设置响应数据类型
		response.setContentType("application/json");
		//接收前端传过来的参数,并且做相应转型处理
		String Mid=request.getParameter("mid");
		String Uid=request.getParameter("uid");
		String contents=request.getParameter("contents");
		int mid=Integer.parseInt(Mid);
		int uid=Integer.parseInt(Uid);
		//执行评论添加到数据库操作
		Comments comments=new Comments(mid, uid, contents);
		Boolean flag=cs.addComments(comments);
		Msg msg=new Msg();
		if(flag) {
			msg.setMsg("评论成功");
		}else {
			msg.setMsg("评论失败");
		}
		//将获得的json格式的数据转成json类型的数据传回去
		Gson gson=new Gson();
		String str=gson.toJson(msg);
		PrintWriter out=response.getWriter();
		out.print(str);
		out.close();
	}
	
	protected void dodelcomments(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//设置响应编码 
		response.setCharacterEncoding("utf-8");
		//设置响应数据类型
		response.setContentType("application/json");
		String Cid=request.getParameter("cid");
		int cid=Integer.parseInt(Cid);
		//System.out.println(cid);
		//删除评论
		Boolean flag=cs.deleteCommentsByCid(cid);
		//将获得的json格式的数据转成json类型的数据传回去
		Gson gson=new Gson();
		Msg msg=new Msg();
		if(flag) {
			msg.setMsg("删除成功");
		}else {
			msg.setMsg("删除失败");
		}
		//将操作的信息传回去
		String str=gson.toJson(msg);
		PrintWriter out=response.getWriter();
		out.print(str);
		out.close();
	}
	
	
	
	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
